/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.by.wms.controller;

import cn.by.wms.dto.LoginDTO;
import cn.by.wms.dto.RegisterDTO;
import cn.by.wms.entity.User;
import cn.by.wms.entity.UserQuery;
import cn.by.wms.service.UserService;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWTUtil;
import cn.smart.core.annotation.LogTimeX;
import cn.smart.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//Restful Api
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping
    public List<User> select(UserQuery query) {
        return userService.select(query);
    }

    @GetMapping("/login")
    public String login(@ModelAttribute LoginDTO loginDTO) {
        System.out.println(666);
        return      userService.login(loginDTO);

    }

     @PostMapping("/register")
    public Integer register(@RequestBody RegisterDTO registerDTO) {
      return  userService.register(registerDTO);
    }


    @GetMapping("/send/code2")
    public  Integer sendCode2(@RequestParam String email) {
      return   userService.sendCode(email);
    }



    @GetMapping("/send/code")
    public  Integer sendCode(@RequestParam String email) {
        MailAccount account = new MailAccount();
        account.setHost("smtp.qq.com");
        account.setPort(465);
        account.setAuth(true);
        account.setFrom("768793659@qq.com");
        account.setUser("768793659@qq.com");
        account.setPass("ukgomqasulibbeae");
        account.setSslEnable(true);
        int code = RandomUtil.randomInt(1000,9999);
//        map.put("code", code);
        MailUtil.send(account, CollUtil.newArrayList(email), "曹哥下班验证码", "【河南公安政务服务平台】"+code+"(3分钟有效)，尊敬的颜如玉用户，您的曹哥需要使用此验证码才能下班，请你告知他验证码助他下班！请勿泄露短信验证码!  ", false);
        return 1;
    }

    @GetMapping("/send/code3")
    public  Integer sendCode3(@RequestParam String email) {
        MailAccount account = new MailAccount();
        account.setHost("smtp.aliyun.com");
        account.setPort(25);
        account.setAuth(true);
        account.setFrom("15893469212@aliyun.com");
        account.setUser("15893469212@aliyun.com");
        account.setPass("mmm");
        int code = RandomUtil.randomInt(1000,9999);
//        map.put("code", code);
        MailUtil.send(account, CollUtil.newArrayList(email), "曹哥下班验证码", "【河南公安政务服务平台】"+code+"(3分钟有效)，尊敬的颜如玉用户，您的曹哥需要使用此验证码才能下班，请你告知他验证码助他下班！请勿泄露短信验证码!  ", false);
        return 1;
    }




}
